EWS - How to search for items [message] between dates ?

Posted by SomFred on Stack Overflow See other posts from Stack Overflow or by SomFred
Published on 2010-03-30T11:51:43Z Indexed on 2010/03/30 11:53 UTC
Read the original article Hit count: 1250

Filed under:
|

Hi,

I am trying to search for message items between two dates from the inbox folder. I use the following restrictionType but it throws this error:

firmt.RootFolder = null

What am I doing wrong?

There is some messages between the mentionned dates ;-)

Thanks for your suggestions.

        using (ExchangeServiceBinding esb = new ExchangeServiceBinding())
        {
            esb.Url = ConfigurationManager.AppSettings["ExchangeWebServicesURL"].ToString();
            esb.RequestServerVersionValue = new RequestServerVersion();
            esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
            esb.PreAuthenticate = true;
            esb.Credentials = new NetworkCredential(email, password);
            FindItemType findItemRequest = new FindItemType();
            // paging
            IndexedPageViewType ipvt = new IndexedPageViewType();
            ipvt.BasePoint = IndexBasePointType.Beginning;
            ipvt.MaxEntriesReturned = nombreMessage;
            ipvt.MaxEntriesReturnedSpecified = true;
            ipvt.Offset = offset;
            findItemRequest.Item = ipvt;
            // filter by dates
            AndType andType = new AndType();
            List<SearchExpressionType> searchExps = new List<SearchExpressionType>();
            RestrictionType restriction = new RestrictionType();
            PathToUnindexedFieldType pteft = new PathToUnindexedFieldType
            {
                FieldURI = UnindexedFieldURIType.itemDateTimeSent
            };
            IsGreaterThanOrEqualToType IsGreaterThanOrEqualTo = new IsGreaterThanOrEqualToType
            {
                Item = pteft,
                FieldURIOrConstant = new FieldURIOrConstantType
                {
                    Item = new ConstantValueType
                    {
                        Value = DateTime.Today.AddDays(-6d).ToString()
                    }
                }
            };
            searchExps.Add(IsGreaterThanOrEqualTo);

            IsLessThanOrEqualToType IsLessThanOrEqualTo = new IsLessThanOrEqualToType
            {
                Item = pteft,
                FieldURIOrConstant = new FieldURIOrConstantType
                {
                    Item = new ConstantValueType
                    {
                        Value = DateTime.Today.AddDays(1d).ToString()
                    }
                }
            };
            searchExps.Add(IsLessThanOrEqualTo);
            andType.Items = searchExps.ToArray();
            restriction.Item = andType;
            findItemRequest.Restriction = restriction;
            //// Define the sort order of items.
            FieldOrderType[] fieldsOrder = new FieldOrderType[1];
            fieldsOrder[0] = new FieldOrderType();
            PathToUnindexedFieldType dateOrder = new PathToUnindexedFieldType
            {
                FieldURI = UnindexedFieldURIType.itemDateTimeReceived
            };
            fieldsOrder[0].Item = dateOrder;
            fieldsOrder[0].Order = SortDirectionType.Descending;
            findItemRequest.SortOrder = fieldsOrder;
            findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
            // define which item properties are returned in the response 
            findItemRequest.ItemShape = new ItemResponseShapeType
            {
                BaseShape = DefaultShapeNamesType.IdOnly
            };
            // identify which folder to search 
            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.inbox };
            // add folders to request 
            findItemRequest.ParentFolderIds = folderIDArray;
            // find the messages 
            FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
            //-------------
            ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages;
            ResponseMessageType responseMessage = responseMessages.Items[0];
            if (responseMessage is FindItemResponseMessageType)
            {
                FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType);
         *******FindItemParentType fipt = firmt.RootFolder;********
                object obj = fipt.Item;
                // FindItem contains an array of items.
                ArrayOfRealItemsType realitems = (obj as ArrayOfRealItemsType);
                ItemType[] items = realitems.Items;
                // if no messages were found, then return null -- we're done 
                if (items == null || items.Count() <= 0)
                    return null;
                // FindItem never gets "all" the properties, so now that we've found them all, we need to get them all. 
                BaseItemIdType[] itemIds = new BaseItemIdType[items.Count()];
                for (int i = 0; i < items.Count(); i++)
                    itemIds[i] = items[i].ItemId;
                GetItemType getItemType = new GetItemType
                {
                    ItemIds = itemIds,
                    ItemShape = new ItemResponseShapeType
                    {
                        BaseShape = DefaultShapeNamesType.AllProperties,
                        BodyType = BodyTypeResponseType.Text,
                        BodyTypeSpecified = true,
                        AdditionalProperties = new BasePathToElementType[] { 
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.itemDateTimeSent },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageFrom },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageIsRead },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageSender },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageToRecipients },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageCcRecipients },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageBccRecipients }
                        }
                    }
                };
                GetItemResponseType getItemResponse = esb.GetItem(getItemType);
                messages = ReadItems(getItemResponse, items.Count());
            }

© Stack Overflow or respective owner

Related posts about c#

Related posts about exchangewebservices